﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Provausio.Common.Utility.Extensions
{
    public static class Objects
    {
        /// <summary>
        /// Converts database object to .net type. On error, returns default value of T. Specify T? return nullable.
        /// </summary>
        /// <typeparam name="T">Expected data type</typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T DbTo<T>(this object value)
        {
            if (value == null || value == DBNull.Value || !(value is T)) return default(T);
            try
            {
                return (T)value;
            }
            catch (Exception)
            {
                return default(T);
            }
        }
    }
}
